package com.fare.taluo.core.domain.thread;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

import com.fare.taluo.conf.ConfigContainer;
import com.fare.taluo.conf.PropertyUtils;
import com.fare.taluo.core.domain.config.city.PortEntity;
import com.fare.taluo.core.domain.config.line.FlightInfoEntity;
import com.fare.taluo.core.domain.crawl.CrawlCSAirLineModel;
import com.fare.taluo.core.domain.crawl.CrawlPropertiesLoader;
import com.fare.taluo.core.domain.crawl.adapter.sub.CEAirLineAdaptor;
import com.fare.taluo.core.domain.entity.pretreatment.QueueLineEntity;

public class CELineThread extends Thread {
    private List<String> portCodes ;
    private CountDownLatch threadsSignal;  
	public CELineThread(List<String> portCodes,CountDownLatch threadsSignal){
    	this.portCodes = portCodes;
    	this.threadsSignal = threadsSignal;
    }
	
	
	public CELineThread(List<String> portCodes){
    	this.portCodes = portCodes;
    }
	@Override
	public void run() {
		       //1获得所有机场信息
				int sum=0;
				ConfigContainer.getInstance().register("com.fare.taluo.core.domain.crawl.CrawlPropertiesLoader");
				PortEntity portEntity = new PortEntity();
				FlightInfoEntity flightEntity = new FlightInfoEntity();
				List<PortEntity> portList =  portEntity.findAll();//获得所有机场
				Map<String,PortEntity> map = portEntity.findPortEntityToMap();//以map的形式保存机场 key:机场中文名称
				//3循环去访问网站尝试
				for (Iterator iterator = portCodes.iterator(); iterator.hasNext();) {
					 String string = (String) iterator.next();
					String[] codes = string.split("-");
					String fromCityCode = codes[0];
					String toCityCode = codes[1];
					//抓取机场模型
					CrawlCSAirLineModel portModel = new CrawlCSAirLineModel();
					portModel.setFromCity(fromCityCode);
					portModel.setToCity(toCityCode);
					portModel.setQueryType("1");
					//ConfigContainer.getInstance().register("com.fare.taluo.core.domain.crawl.CrawlPropertiesLoader");
					CEAirLineAdaptor adap = new CEAirLineAdaptor(portModel);
					Object obj = null;
					
					int fetchCount = PropertyUtils.getIntProperty("crawl.line.fail.repeat.times", CrawlPropertiesLoader.CONFIG_FILE);
					try{
						while(fetchCount > 0){
						    obj = adap.fetch(null);
						    fetchCount--;				    
						    if(null!= obj ){	
								 List<Object> results = adap.paraseToVo(obj.toString(),map);
								 if(null == results){		
						     		System.out.println(fromCityCode+"-"+toCityCode);
									break;			
								 }
								
								//循环保存航班信息
								if(null != results && results.size()>0){	
									for (Iterator iterator2 = results.iterator(); iterator2
											.hasNext();) {
										FlightInfoEntity entity = (FlightInfoEntity) iterator2.next();
										if(null!= map.get(entity.getFromCity())&& null!= map.get(entity.getToCity())){
											flightEntity.tranSave(entity);
										}
										
									}
								}
							}
						    
						}
						
					} catch (Exception e1) {
						e1.printStackTrace();
					}
					
					
					QueueLineEntity.ceCount();
					
				}
				System.out.println("---------finished---------");
				threadsSignal.countDown();
				ThreadFlag.setIsFinish(true);
	}
}
